//
//  DataBaseSingleton.m
//  豆瓣2
//
//  Created by dllo on 15/12/18.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "DataBaseSingleton.h"
#import <sqlite3.h>
#import "Movie.h"
@implementation DataBaseSingleton

+(instancetype)shareDataBase{
    
    static DataBaseSingleton *dataBase = nil;
    
    if (dataBase==nil) {
        
        dataBase = [[DataBaseSingleton alloc]init];
        
    }
    
    return dataBase;
}

static sqlite3 *db = nil;

- (void)openOB{
    
    if (db != nil) {
        
        NSLog(@"数据库已经打开");
    }
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *filPath = [documentPath stringByAppendingPathComponent:@"Movie.sqlite"];
    
    NSLog(@"%@",filPath);
    
    BOOL result = sqlite3_open(filPath.UTF8String, &db);
    
    if (result==SQLITE_OK) {
        
        NSLog(@"打开成功");
    }else{
        
        NSLog(@"打开失败");
    }
    
}
// 创建电影表格
- (void)createTable{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS movie1028(number INTEGER PRIMARY KEY AUTOINCREMENT,movieName TEXT,movieId TEXT,pic_url TEXT)";
    
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result==SQLITE_OK) {
        
        NSLog(@"---创建成功");
    }else{
        
        NSLog(@"创建失败");
    }
    
}
// 插入表格
- (void)inserMovieIntoTable:(Movie *)movie{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO movie1028(movieName,movieId,pic_url) VALUES('%@','%@','%@')",movie.movieName,movie.movieId,movie.pic_url];
    
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result==SQLITE_OK) {
        
        NSLog(@"插入成功");
    }else{
        
        NSLog(@"插入失败");
    }
    
}
// 查询

- (NSMutableArray *)selectMovie{
    
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT *FROM movie1028"];
    
    sqlite3_stmt *stmt = nil;
    
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result==SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        
        NSLog(@"查询失败");
        
        return nil;
    }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        
        const unsigned char *charName = sqlite3_column_text(stmt, 1);
         const unsigned char *charMovieId = sqlite3_column_text(stmt, 2);
        const unsigned char *charimage = sqlite3_column_text(stmt, 3);
        NSString *name = [NSString stringWithUTF8String:(const char *)charName];
        NSString *movieId = [NSString stringWithUTF8String:(const char *)charMovieId];
        NSString *image = [NSString stringWithUTF8String:(const char *)charimage];

        Movie *movie = [[Movie alloc]init];
        movie.movieName = name;
        movie.movieId = movieId;
        movie.pic_url = image;
        NSLog(@"-------%@",name);
        
        [array addObject:movie];
    }
    
    return array;
}

// 删除

- (void)dropTable{
    
    NSString *sql = @"DROP TABLE movie1028";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result==SQLITE_OK) {
        
        NSLog(@"删除成功");
    }else{
        
        NSLog(@"删除失败");
    }
    
}


// 创建收藏表格
- (void)openuser{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS user1028(number INTEGER PRIMARY KEY AUTOINCREMENT,movieName TEXT,movieId TEXT)";
    
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result==SQLITE_OK) {
        
        NSLog(@"---创建成功");
    }else{
        
        NSLog(@"创建失败");
    }

    
}

- (void)inserUserIntoTable:(Movie *)movie{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO user1028(movieName,movieId) VALUES('%@','%@')",movie.movieName,movie.movieId];
    
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result==SQLITE_OK) {
        
        NSLog(@"插入成功");
    }else{
        
        NSLog(@"插入失败");
    }
    
}

- (NSMutableArray *)selectUser{
    
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT *FROM user1028"];
    
    sqlite3_stmt *stmt = nil;
    
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result==SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        
        NSLog(@"查询失败");
        
        return nil;
    }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        
        const unsigned char *charName = sqlite3_column_text(stmt, 1);
        const unsigned char *charMovieId = sqlite3_column_text(stmt, 2);
      
        NSString *name = [NSString stringWithUTF8String:(const char *)charName];
        NSString *movieId = [NSString stringWithUTF8String:(const char *)charMovieId];
        
        Movie *movie = [[Movie alloc]init];
        movie.movieName = name;
        movie.movieId = movieId;
        NSLog(@"-------%@",name);
        
        [array addObject:movie];
    }
    
    return array;
}


@end
